#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include <mypushbutton.h>
#include <QDebug>
#include <QTimer>
#include <QSound>

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    // 设置固定大小
    this->setFixedSize(320,588);
    // 设置应用图片
    this->setWindowIcon(QPixmap( ":/res/Coin0001.png"));
    // 标题
    this->setWindowTitle("老帮主带你翻金币");

    // 退出按钮
    connect(ui->actionquit, &QAction::triggered, [=](){this->close();});

    QSound *startSound = new QSound(":/res/TapButtonSound.wav", this);

    // 开始按钮
    MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");

    startBtn->setParent(this);
    startBtn->move(this->width()/2-startBtn->width()/2, this->height()*0.7);

    chooseScene = new ChooseLeveScene;

    // 监听选择关卡返回按钮信号
    connect(chooseScene, &ChooseLeveScene::chooseSceneBack, this, [=](){
        this->setGeometry(chooseScene->geometry());
        chooseScene->hide();
        this->show();

    });

    connect(startBtn, &MyPushButton::clicked, [=](){
       //qDebug()<<"点击";
        // 播放音效
       startSound->play();
       startBtn->zoom1();
       startBtn->zoom2();

       // 延时进入到选择关卡
       QTimer::singleShot(300, this, [=](){
           // 设置一下chooseScene位置
           chooseScene->setGeometry(this->geometry());
           // 自己隐藏
           this->hide();
           // 进入选择关卡场景中
           chooseScene->show();
       });
    });
}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,width(), height(),pix);

    // 背景图标
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()/2, pix.height()/2);
    painter.drawPixmap(10,30,pix);
}

MainScene::~MainScene()
{
    delete ui;
}

